package com.example.zg.ui_demo;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.zg.ui_demo.fragments.CollectionFragment;
import com.example.zg.ui_demo.fragments.SearchFragment;
import com.example.zg.ui_demo.fragments.ShopFragment;
import com.example.zg.ui_demo.fragments.UserFragment;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity
        implements BottomNavigationView.OnNavigationItemReselectedListener,ViewPager.OnPageChangeListener, BottomNavigationView.OnNavigationItemSelectedListener {

    private ViewPager viewPager;
    private BottomNavigationView bottomNavigationView;

    private FragmentPagerAdapter adapter;

    private List<Fragment> fragmentList;

    private String UserId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        viewPager = findViewById(R.id.viewPager);
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        UserId=getIntent().getStringExtra("user");
        System.out.println("UserId:"+UserId);
        Toast.makeText(getApplicationContext(),"UserId-->"+UserId,Toast.LENGTH_SHORT).show();

        // 定义 4 个 Fragment ，并加⼊入 List 中
        fragmentList = new ArrayList<>();
        fragmentList.add(new ShopFragment());
        fragmentList.add(new CollectionFragment());
        fragmentList.add(new SearchFragment());
        fragmentList.add(new UserFragment());

        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                // 根据位置返回对应的 Fragment
                // 0 对应 ShopFragment ， 1 对应 CollectionFragment 。。。
                return fragmentList.get(i);
            }

            @Override
            public int getCount() {
                //返回fragment数量
                return fragmentList.size();
            }
        };
        //添加适配器
        viewPager.setAdapter(adapter);
        // 给 ViewPager 添加监听器器，当前类需要实现 OnPageChangeListener 接⼝口
        viewPager.addOnPageChangeListener(this);
        // 给 BottomNavigationView 添加监听器器，当前类需要实现 OnNavigationItemSelectedListener 接⼝
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
        // 选中 " 店铺 " 导航按钮
        bottomNavigationView.setSelectedItemId(R.id.nav_shop);
    }
    // 监听 BottomNavigationView

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        int id = 0;
        // 根据底部导航菜单项的 id ，指定 ViewPager 中的 Fragment

        switch (menuItem.getItemId()){
            case R.id.nav_shop:
                id = 0;
                break;
            case R.id.nav_collection:
                id = 1;
                break;
            case R.id.nav_search:
                id = 2;
                break;
            case R.id.nav_user:
                id = 3;
                break;
        }
        // 指定 ViewPager 中的 Fragment
        System.out.println("fanhuiNa:"+id);
        viewPager.setCurrentItem(id);
        return true;
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        int id=0;
        switch (i) {
            case R.id.nav_shop:
                id = 0;
                break;
            case R.id.nav_collection:
                id = 1;
                break;
            case R.id.nav_search:
                id = 2;
                break;
            case R.id.nav_user:
                id = 3;
                break;
        }
        System.out.println("fanhuiPa:"+id);
        bottomNavigationView.setSelectedItemId(id);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onNavigationItemReselected(@NonNull MenuItem item) {

    }
}
